package com.baizhi.action;

import com.baizhi.domain.Result;
import com.baizhi.entity.Category;
import com.baizhi.service.CategoryService;
import com.baizhi.service.impl.CategoryServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.UUID;

public class CategoryAction {
    private Result result;
    private Integer pageNum;
    private Integer pageSize;
    private PageInfo<Category> pageInfo;

    private Integer levels;

    private String categoryName;

    private List<Category> categories;
    private String parentId;
    private String categoryId;

    private String message;

    public Result getResult() {
        return result;
    }

    public void setResult(Result result) {
        this.result = result;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public PageInfo<Category> getPageInfo() {
        return pageInfo;
    }

    public void setPageInfo(PageInfo<Category> pageInfo) {
        this.pageInfo = pageInfo;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    public List<Category> getCategories() {
        return categories;
    }

    public void setCategories(List<Category> categories) {
        this.categories = categories;
    }

    public String getParentId() {
        return parentId;
    }

    public String getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(String categoryId) {
        this.categoryId = categoryId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Integer getLevels() {
        return levels;
    }

    public void setLevels(Integer levels) {
        this.levels = levels;
    }

    public String selectAllCategoryByPages(){
        CategoryService categoryService=new CategoryServiceImpl();
        if (pageNum==null){
            pageNum=1;
        }
        if (pageSize==null){
            pageSize=5;
        }
        PageHelper.startPage(pageNum,pageSize);
        categories = categoryService.selectAllCategory();
        pageInfo=new PageInfo<>(categories,5);
        return "success";
    }

    public String addCategoryLevels1() throws UnsupportedEncodingException {
        CategoryService categoryService=new CategoryServiceImpl();
        String uuid = UUID.randomUUID().toString();
        System.out.println(uuid);
        Category category1 = categoryService.selectCategoryByName(categoryName);
        if (category1==null){
            Category category = new Category(uuid, categoryName, 1, null);
            categoryService.addCategoryLevels1(category);
            return "showCategoryList";
        }else {
            message= URLEncoder.encode( "当前一级类别名已有，不能重复添加","utf-8");
            return "addCategoryLevels1Error";
        }

    }

    public String toAddSecondCategory(){
        CategoryService categoryService=new CategoryServiceImpl();
        categories = categoryService.selectCategoryLevels1();
        return "addSecondCategory";
    }

    public String addCategoryLevels2() throws UnsupportedEncodingException {
        CategoryService categoryService=new CategoryServiceImpl();
        String uuid = UUID.randomUUID().toString();
        System.out.println(uuid);
        Category category1 = categoryService.selectCategoryByName(categoryName);
        if(category1==null){
            Category parent = new Category();
            parent.setCategoryId(parentId);
            Category category = new Category(uuid, categoryName, 2, parent);
            categoryService.addCategoryLevels2(category);
            return "showCategoryList";
        }else {
            message= URLEncoder.encode( "当前二级类别名已有，不能重复添加","utf-8");
            return "addCategoryLevels2Error";
        }

    }

    public String deleteCategory() throws UnsupportedEncodingException {
        CategoryService categoryService=new CategoryServiceImpl();
        Integer count = categoryService.selectCountForLevelsById(levels, categoryId);
        if (count==0){
            categoryService.deleteCategory(categoryId);
        }else {
            message= URLEncoder.encode( "当前类别下有关联，不能删除","utf-8");
            return "deleteCategoryError";
        }
        return "showCategoryList";
    }
}
